cmake_minimum_required(VERSION 3.8)
project(mindvision_camera)

## 设置C++标准为C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

## 编译选项：启用严格警告检查
# -Wall：显示所有警告信息
# -Werror：将警告视为错误，强制开发者修复潜在问题，保证代码质量
add_definitions(-Wall -Werror)

## 导出编译命令（compile_commands.json）
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# --------------------------
#  依赖
# --------------------------

# 使用ament_cmake_auto简化ROS 2依赖管理
# 自动查找package.xml中声明的依赖项（如rclcpp、sensor_msgs等）
find_package(ament_cmake_auto REQUIRED)
ament_auto_find_build_dependencies()

# libusb-1.0 的查找
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBUSB REQUIRED libusb-1.0)

# --------------------------
#  构建
# --------------------------

# 生成共享库（.so文件），库名与项目名一致
# 源文件包括节点实现（mv_camera_node.cpp）和SDK封装（mv_camera_sdk.cpp）
ament_auto_add_library(${PROJECT_NAME} SHARED
  src/mv_camera_node.cpp
  src/mv_camera_sdk.cpp
)

# 指定头文件搜索路径
target_include_directories(${PROJECT_NAME} PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>  # 编译时的头文件路径
  $<INSTALL_INTERFACE:include>                            # 安装后的头文件路径
  mvsdk/include                                           # MindVision SDK的头文件
)

# 根据处理器架构链接对应的SDK库
# x86_64（PC）和aarch64（嵌入式，如Jetson）分别使用不同目录的库文件
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
  target_link_directories(${PROJECT_NAME} PUBLIC mvsdk/lib/amd64)
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  target_link_directories(${PROJECT_NAME} PUBLIC mvsdk/lib/arm64)
else()
  message(FATAL_ERROR "Unsupported host system architecture: ${CMAKE_HOST_SYSTEM_PROCESSOR}!")
endif()

# 链接MindVision SDK的核心库（MVSDK）
target_link_libraries(${PROJECT_NAME} MVSDK ${LIBUSB_LIBRARIES})

# --------------------------
# 注册组件化节点
# --------------------------

# 插件：指定节点类（mindvision_camera::MVCameraNode）
# 可执行文件：生成节点运行程序（mindvision_camera_node）
rclcpp_components_register_node(${PROJECT_NAME}
  PLUGIN mindvision_camera::MVCameraNode
  EXECUTABLE ${PROJECT_NAME}_node
)

# --------------------------
#  测试
# --------------------------

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  list(APPEND AMENT_LINT_AUTO_EXCLUDE
    ament_cmake_copyright
    ament_cmake_cpplint
    ament_cmake_uncrustify
  )
  ament_lint_auto_find_test_dependencies()

  set(_linter_excludes
    mvsdk/include/CameraApi.h
    mvsdk/include/CameraDefine.h
    mvsdk/include/CameraStatus.h
  )
  ament_cpplint(EXCLUDE ${_linter_excludes})
endif()

# --------------------------
# 安装配置
# --------------------------

# 安装库文件
install(TARGETS ${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

# 安装头文件
install(DIRECTORY include/
  DESTINATION include/${PROJECT_NAME}
)

# 安装配置和启动文件
install(DIRECTORY
  config
  launch
  DESTINATION share/${PROJECT_NAME}
)

# 安装 SDK 文件
install(DIRECTORY mvsdk/include/
  DESTINATION share/${PROJECT_NAME}/mvsdk/include
)

# 安装 SDK 库文件（根据架构选择）
if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64")
  install(DIRECTORY mvsdk/lib/amd64/
    DESTINATION lib 
    USE_SOURCE_PERMISSIONS
    FILES_MATCHING PATTERN "*.so*"  
  )
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
  install(DIRECTORY mvsdk/lib/arm64/
    DESTINATION lib 
    USE_SOURCE_PERMISSIONS
    FILES_MATCHING PATTERN "*.so*"
  )
endif()

ament_package()
